import {messages} from "share/common";
/**
 * Created by 13576 on 2017/10/20.
 */
import React from 'react'
import { connect } from 'react-redux'
import { Popover, Table, Badge} from 'antd';
import config from 'config'
import menuRoute from 'routes/menuRoute'
import SearchArea from 'components/search-area';
import budgetJournalService from 'containers/budget/budget-journal-re-check/budget-journal-re-check.service'
import ResizeTable from 'components/resize-table'

class BudgetJournalReCheck extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      data: [],
      searchParams: {
        journalCode: '',
        journalTypeId: '',
        periodStrategy: '',
        versionId: '',
        structureId: '',
        scenarioId: '',
        employeeId: '',
        createDate: '',
      },
      params: {
        journalCode: '',
        journalTypeId: '',
        periodStrategy: '',
        versionId: '',
        structureId: '',
        scenarioId: '',
        employeeId: '',
        createDate: ''
      },
      pagination: {
        total: 0,
      },
      page: 0,
      pageSize: 10,
      searchForm: [
        {type: 'input', id: 'journalCode', label: messages('budget.key102'/*预算日记账编号*/)},
        {type: 'select', id:'journalTypeId', label: messages('budget.key105'/*预算日记账类型*/), options: [], method: 'get',
          getUrl: `${config.budgetUrl}/api/budget/journals/journalType/selectByInput`, getParams: {organizationId: props.organization.id},
          labelKey: 'journalTypeName', valueKey: 'id'},
        {type:'value_list',label: messages('budget.key109'/*编制期段*/) ,id:'periodStrategy',options: [], valueListCode: 2002},
        {type: 'select', id:'versionId', label: messages('budget.key108'/*版本*/), options: [], method: 'get',
          getUrl: `${config.budgetUrl}/api/budget/versions/queryAll`, getParams: {organizationId: props.organization.id},
          labelKey: 'versionName', valueKey: 'id'},
        {type: 'select', id:'structureId', label: messages('budget.key106'/*预算表*/),  options: [], method: 'get',
          getUrl: `${config.budgetUrl}/api/budget/structures/queryAll`, getParams: {organizationId: props.organization.id},
          labelKey: 'structureName', valueKey: 'id'},
        {type: 'select', id:'scenarioId', label: messages('budget.key107'/*场景*/), options: [], method: 'get',
          getUrl: `${config.budgetUrl}/api/budget/scenarios/queryAll`, getParams: {organizationId: props.organization.id},
          labelKey: 'scenarioName', valueKey: 'id'},
        {type: 'select', id:'employeeId',label: messages('budget.key103'/*申请人*/), options: [], method: 'get',
          getUrl: `${config.budgetUrl}/api/budget/journals/selectCheckedEmp`, getParams: {},
          labelKey: 'empName', valueKey: 'empOid'},
        {type:'date',id:'createDate', label:messages('budget.key104'/*创建时间*/)}
      ],
      columns: [
        {
          title: messages('budget.key102'/*预算日记账编号*/), key: "journalCode", dataIndex: 'journalCode',width: '18%',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key105'/*预算日记账类型*/), key: "journalTypeName", dataIndex: 'journalTypeName',width: '12%',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key109'/*编制期段*/), key: "periodStrategyName", dataIndex: 'periodStrategyName'
        },
        {
          title: messages('budget.key106'/*预算表*/), key: "structureName", dataIndex: 'structureName',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key107'/*场景*/), key: "scenario", dataIndex: 'scenario',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key108'/*版本*/), key: "versionName", dataIndex: 'versionName',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key103'/*申请人*/), key: "employeeName", dataIndex: 'employeeName',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key104'/*创建时间*/), key: "createdDate", dataIndex: 'createdDate',
          render: value => <Popover content={value}>{value}</Popover>
        },
        {
          title: messages('budget.key101'/*状态*/), key: "status", dataIndex: 'status',
          render: (value, record) => {
            const statusObj = {
              NEW: 'processing',
              SUBMIT: 'default',
              SUBMIT_RETURN: 'default',
              REJECT: 'error',
              CHECKED: 'warning',
              CHECKING: 'warning',
              POSTED: 'success',
              BACKLASH_SUBMIT: 'default',
              BACKLASH_CHECKED: 'default'
            };
            return <Badge status={statusObj[value]} text={record.statusName}/>
          }
        },
      ],
      selectedEntityOIDs: []    //已选择的列表项的OIDs
    };
  }

  componentWillMount() {
    this.getList();
  }

  //获取复核
  getList() {
    const { page, pageSize } = this.state;
    let params = Object.assign({}, this.state.searchParams);
    params.organizationId = this.props.organization.id;
    params.page = page;
    params.size = pageSize;
    this.setState({ loading:true });
    budgetJournalService.getBudgetJournalReCheckHeader(params).then(response => {
      this.setState({
        loading: false,
        data: response.data,
        pagination: {
          total: Number(response.headers['x-total-count'] || 0),
          onChange: this.onChangePager,
          current: this.state.page + 1
        }
      })
    })
  }

  //分页点击
  onChangePager = (page) => {
    if (page - 1 !== this.state.page)
      this.setState({
        page: page - 1,
        loading: true
      }, () => {
        this.getList();
      })
  };

  //点击搜搜索
  handleSearch = (values) => {
    let valuesData = {
      ...this.state.params,
      ...values,
      createDate: values['createDate'] ? values['createDate'].format('YYYY-MM-DD') : '',
    };
    this.setState({ searchParams: valuesData }, this.getList)
  };

  //新建
  handleCreate = () => {
    let path = this.state.newBudgetJournalDetailPage.url;
    this.context.router.push(path)
  };

  //跳转到详情
  HandleRowClick = (value) => {
    const journalCode = value.id;
    let detailPage = menuRoute.getRouteItem('budget-journal-re-check-detail'); //预算日记账复核详情
    let path = detailPage.url.replace(":journalCode", journalCode);
    this.context.router.push(path);
  };

  render() {
    const { loading, searchForm ,data, pagination, columns } = this.state;
    return (
      <div className="budget-journal">
        <SearchArea searchForm={searchForm} submitHandle={this.handleSearch}/>
        <div className="table-header">
          <div className="table-header-title">{messages('common.total', {total: `${pagination.total}`})}</div>
        </div>
        <ResizeTable
          loading={loading}
          dataSource={data}
          columns={columns}
          pagination={pagination}
          size="middle"
          bordered
          onRow={record => ({
            onClick: () => this.HandleRowClick(record)
          })}
          rowKey={record => record.id}
        />
      </div>
    )
  }

}

BudgetJournalReCheck.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    organization: state.login.organization
  }
}

export default connect(mapStateToProps)(BudgetJournalReCheck);
